import { Directive, OnInit, Input, OnDestroy } from '@angular/core';

import { LoggerService, ComponentBase } from '../share';

let nextId = 1;

// Spy on any element to which it is applied.
// Usage: <div mySpy>...</div>
@Directive({
    selector: '[mySpy]'
})
export class SpyDirective extends ComponentBase implements OnInit, OnDestroy {

    private _mySpyName: string;

    constructor(private logger: LoggerService) {
        super();
    }

    ngOnInit() {
        super.ngOnInit();
        this.logIt(`onInit`);
    }

    ngOnDestroy() {
        super.ngOnDestroy();
        this.logIt(`onDestroy`);
    }

    @Input('mySpy') get mySpyName() {
        this.internalConsoleLog('get mySpyName');
        return this._mySpyName;
    }

    set mySpyName(value: string) {
        this.internalConsoleLog('set mySpyName');
        this._mySpyName = value;
    }

    private logIt(msg: string) {
        this.logger.log(`Spy #${nextId++} ${msg}`);
    }
}